﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
// Copyright 2009, 2010 Matvei Stefarov <me@matvei.org>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ForgeCraft
{
    public class CmdItem : Command
    {
        public override string name { get { return "item"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "player"; } }
        public override bool consoleUsable { get { return false; } }
        public override Permission defaultRank { get { return Permission.Guest; } }
        public CmdItem() { }

        public override void Use(Player p, string message)
        {
            string strType = message.Split(' ')[0];

            short damage = 0;

            if (strType.Split(':').Length > 1)
            {
                short.TryParse(strType.Split(':')[1], out damage);
            }

            strType = strType.Split(':')[0];

            byte amount = 1;

            if (message.Split(' ').Length > 1)
            {
                byte.TryParse(message.Split(' ')[1], out amount);
            }

            short type = (short)Block.Air;
            
            if (strType == null)
            {
                p.Message(Color.Announcement + "You must give either the name of the item or its ID number");
                return;
            }

            switch (strType.ToLower())
            {
                case "hellstone": 
                case "bloodstone": 
                case "redmossycobblestone":
                case "netherstone": 
                    strType = "netherrack"; break;
                case "slowsand":
                case "quicksand":
                case "mud":
                case "hellmud":
                case "nethermud":
                    strType = "soulsand"; break;
                case "wheat": strType = "crops"; break;
                case "lapis":
                case "lapiz":
                    strType = "dye"; damage = (byte)Metadata.Dyes.LapisLazuli; break;
                case "lapizblock":
                    strType = "lapisblock"; break;
                case "lapizore":
                    strType = "lapisore"; break;
                case "lightstone":
                case "brittlegold":
                case "brightstone":
                case "australium":
                case "brimstone":
                    strType = "glowstone"; break;
            }

            foreach (Block block in Enum.GetValues(typeof(Block)))
            {
                if (block.ToString().ToLower() != strType.ToLower())
                    continue;
                type = (short)block;
                break;
            }

            foreach (Item item in Enum.GetValues(typeof(Item)))
            {
                if (item.ToString().ToLower() != strType.ToLower())
                    continue;
                type = (short)item;
                break;
            }

            if (type == (byte)Block.Air)
            {
                if (!short.TryParse(strType, out type))
                {
                    p.Message(Color.Announcement + "You must give either the name of the item or its ID number");
                    return;
                }
            }

            //Get tool durability
            /*if (type > 255)
            {
                short durability = -1;

                Items.ToolDuarability.TryGetValue((Item)type, out durability);
                if (durability > 0)
                {
                    if (info.type == (Item)type)
                    {
                        damage = durability;
                        break;
                    }
                }
            }*/

            p.Inventory.AddItem(new InventoryItem(type, amount, damage));

            if (type > 255)
                p.Message(Color.Announcement + "You have been given " + amount + " " + ((Item)type).ToString());
            else
                p.Message(Color.Announcement + "You have been given " + amount + " " + ((Block)type).ToString());
        }
        public override void Help(Player p)
        {
            p.Message(Color.Aqua + "/item <name/id>:<meta> [quantity]");
            p.Message(Color.Help + "Gives you [quantity] blocks or items of type <name/id>:<meta>.");
        }
    }
}
